﻿   //first load google map on body
   function initgooglemap(x,y,mapName) {
      var myCenter=new google.maps.LatLng(x,y);
	  var myOptions={
	     zoom:18,
		 center:myCenter,
		 mapTypeId:google.maps.MapTypeId.ROADMAP
	  }	  
	  var map=new google.maps.Map(document.getElementById("maps"),myOptions);
	   createMarker(myCenter,mapName,map)
   }
   //create google map marker
   function createMarker(googleXY,mapName,mymap) {
	   var image_company = 'http://google-maps-icons.googlecode.com/files/company.png';
	var shadow = new google.maps.MarkerImage('http://google-maps-icons.googlecode.com/files/shadow.png');
	var marker = new google.maps.Marker({
	position: googleXY,
	map: mymap,
	icon: image_company,
	shadow: shadow,
	});	
	var infowindow = new google.maps.InfoWindow({
	maxWidth:200,
	content: mapName
	//size: new google.maps.Size(80,30),
	});

	google.maps.event.addListener(marker, 'click', function() {
	infowindow.open(mymap,marker);
	});
   }

$(document).ready(function() {
    //Get Member Info if page load
	$.ajax({
	  url: 'MB/MyMember',
	  success: function(result) {
		if (result.success==true) {
		loginFormUIchange(result.o.memberName);
		}
	  }
	});

    //addBookmark
    $('#addBookmark').click(function(event) {
		//e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
		var bookmarkUrl = this.href;
		var bookmarkTitle = this.title;
	    if(window.sidebar){ 
				window.sidebar.addPanel(bookmarkTitle, bookmarkUrl, ""); 
		} else if(document.all){ 
				window.external.AddFavorite(bookmarkUrl, bookmarkTitle); 
		} else if(window.opera && window.print){ 
				alert('Press ctrl+D to bookmark (Command+D for macs) after you click Ok'); 
		} else if(window.chrome){ 
				alert('Press ctrl+D to bookmark (Command+D for macs) after you click Ok'); 
		} 
    });
	//Show member register Form
	$('#mbreg').click(function() {  
	
       $('#dlg-mbreg').dialog('open').dialog('setTitle','Member register');  
       $('#fm-mbreg').form('clear');  
       url = 'MB/CreateMB';  
    } );
	//member register
	$('#createMember').click(function() {     
       $('#fm-mbreg').form('submit',{  
        url: url,  
        onSubmit: function(){  
            return $(this).form('validate');  
        },  
        success: function(result){  
            
               if (result.success==false){  
                 $.messager.show({  
                     title: 'Error',  
                     msg: result.errorMsg  
                 });  
               } else {  
            	   alert('You register success');
                   $('#dlg-mbreg').dialog('close');      // close the dialog  
                
               }  
            }  
        });  
    });  
    //add click event to locbar for google map
    $('#locbar>a').each(function(){
	   //console.log($(this).text());
	   $(this).live('click',function(event) {
	       //console.log($(this).attr("x"));
	       initgooglemap($(this).attr("x"),$(this).attr("y"),$(this).text());
	   });
    });	
    //login form reset
	$('#resetFM').click(function() {
	    $('#login_form').find(':input').each(function(){

		   $(this).val('');
		})
	});
    //login form submit action
	$("#mbLogin").click(function() {	
	    url= $('#login_form').attr("url");
		$('#login_form').form('submit',{  
			url: url,  
			onSubmit: function(){  
				var isValid = $(this).form('validate');
				//console.log(isValid+' post ' +url);
				return isValid;  
			},  
			success: function(result){  	
				var result = eval('('+result+')');  
				$.messager.alert(result.title,  result.message,result.title);
				loginFormUIchange(result.o.memberName);
			}  
		});
	});
	//login form change after user login
	function loginFormUIchange(memberName) {
	  $(".logintr1>.tdright").html('<span id="member">'+memberName+'</span>');
	  $(".logintr2").html('<td colspan=2>Welcome you</td>');
	  $(".logintr3").html('<td colspan=2><a id="logoutBN" href="#">Logout</a></td>');
	 
	 //logout
	$("#logoutBN").click(function() {	
	
	$.ajax({
		  url: "MB/Logout",
		  type: "GET",
		  dataType: "json",
		  success: function(result) {  
			$.messager.alert(result.title,  result.message,result.title);
			window.location.replace("/BookingSystem");
		  },	 
		  error: function() {
			alert("Get JSON ERROR!!!");
		  }
		});

	});
	
	}
	//Get current member information
	$("#memberinfo").click(function() {	
	    url= $('#memberinfo').attr("url");
		
		
		 //Get Member Info
	     $.ajax({
	        url: 'MB/MyMember',
	       success: function(result) {
		    if (result.success==true) {
		       $.messager.alert("MemberInfo",  "Member Name : "+result.o.memberName+"<BR>"
			   +"Member Bonus Point : "+result.o.bonusPoint+"<BR>"
			   +"Member Age : "+result.o.memberAge+"<BR>"
			   +"Member Email : "+result.o.email+"<BR>"
			   +"Member Role : "+result.o.role+"<BR>");
	    	}
			else { alert("You never Login!");
			}
	     }
	   });
	
	});
	//footer button example
    $('#footer_right>a').each(function(){
	
	   $(this).live('click',function(event) {
	      $.messager.alert($(this).text(),$(this).text());
	   });
    });		
 });
